//
//  cartTableViewCell.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/11.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "cartTableViewCell.h"
#import "configddd.h"
#import <Masonry/Masonry.h>

@implementation cartTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
        _arr = [[NSMutableArray alloc]init];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editNotification:) name:@"edit" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addShopNotification:) name:@"addShop" object:nil];
        
        self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(185))];
        self.backView.backgroundColor=[UIColor whiteColor];
        [self addSubview:self.backView];
        
        // 选择按钮
        self.selectBtn=[[UIButton alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(75), screenScale(35), screenScale(35))];
        [self.selectBtn setImage:[UIImage imageNamed:@"duihao"] forState:UIControlStateSelected];
        [self.selectBtn setImage:[UIImage imageNamed:@"circile_b"] forState:UIControlStateNormal];
        [self.selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.selectBtn.selected = YES;
        [self.backView addSubview:self.selectBtn];
        
        // 图片
        self.imageShowView=[[UIImageView alloc]initWithFrame:CGRectMake(screenScale(71), screenScale(15), screenScale(150), screenScale(150))];
        self.imageShowView.backgroundColor=[UIColor grayColor];
        [self.backView addSubview:self.imageShowView];
        
        //商品名称
        self.nameLable=[[UILabel alloc]init];
//        self.nameLable.backgroundColor=[UIColor yellowColor];
        self.nameLable.numberOfLines = 0;
        self.nameLable.font = get4sFont(12);
        [self.backView addSubview:self.nameLable];
        [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).with.offset(screenScale(240));
            make.top.mas_equalTo(self).with.offset(screenScale(30));
            make.right.mas_equalTo(self).with.offset(screenScale(-30));
            
//            make.height.mas_equalTo(80);
        }];
        
        //商品价格
        self.moneyLable = [[UILabel alloc]initWithFrame:CGRectMake(screenScale(240), screenScale(135), screenScale(24), screenScale(24))];
        self.moneyLable.text = @"¥";
        self.moneyLable.font = get4sFont(11);
        [self.backView addSubview:self.moneyLable];
        
        self.priceLable=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(265), screenScale(135), screenScale(100), screenScale(24))];
        self.priceLable.font = get4sFont(11);
        self.priceLable.textColor=[UIColor blackColor];
//        self.priceLable.backgroundColor=[UIColor yellowColor];
        [self.backView addSubview:self.priceLable];
        
        self.plainStepper=[[PKYStepper alloc]initWithFrame:CGRectMake(screenScale(465),screenScale(118) , screenScale(155), screenScale(50))];
        
    }
    return self;
}

#pragma mark - notification

- (void)editNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"edit"]isEqualToString:@"yes"]) {
        
        _isEditor = YES;
        
    }else{
        
        _isEditor = NO;
        
    }
    
    NSLog(@"isedit===%hhu",_isEditor);
}

//增加商品数量

- (void)addShopNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"selected"]isEqualToString:@"yes"]) {
        
        if ([_arr[0]isEqualToString:text.userInfo[@"shop_id"]]) {
            
            //替换数组之后 用新的数组给旧的数组赋值---贼几把奇怪
            NSMutableArray *arr = [[NSMutableArray alloc]initWithArray:_arr];
            NSString *str = [NSString stringWithFormat:@"%d",[arr[1] intValue] + 1 ];
            [arr replaceObjectAtIndex:1 withObject:str];
            _arr = arr;
        }
    }else{
        
        if ([_arr[0]isEqualToString:text.userInfo[@"shop_id"]]) {
            
            NSMutableArray *arr = [[NSMutableArray alloc]initWithArray:_arr];
            NSString *str = [NSString stringWithFormat:@"%d",[arr[1] intValue] - 1 ];
            [arr replaceObjectAtIndex:1 withObject:str];
            _arr = arr;
        }
    }
}

- (void)setIsEditor:(Boolean)isEditor{
    
    _isEditor = isEditor;
}

- (void)setArr:(NSMutableArray *)arr{
    
    _arr = arr;
    
    //商品数量
    
    _plainStepper.value = [_shop_num floatValue];
    self.plainStepper.valueChangedCallback = ^(PKYStepper *stepper, float count) {
        
        stepper.countLabel.text = [NSString stringWithFormat:@"%@", @(count)];
        
    };
    _plainStepper.shopid = arr[0];
    _plainStepper.value = [arr[1] floatValue];
    
    [self.plainStepper setup];
    [self.backView addSubview:self.plainStepper];
}

-(void)selectBtnClick:(UIButton*)sender{
    NSLog(@"选择按钮");
    if (sender.isSelected) {
        
        sender.selected=NO;
    }else{
        
        sender.selected=YES;
    }
    
    cartTableViewCell *cell = [(cartTableViewCell *)[self.selectBtn superview] superview];
    NSIndexPath *indexPath=[self.myViewController.cartTableView indexPathForCell:cell];
    NSLog(@"该商品被点击:%ld        %ld",(long)indexPath.section,(long)indexPath.row);
//    [self.myViewController itemSelect:indexPath select:self.selectBtn.isSelected num:self.plainStepper.value];
    
    NSLog(@"---iseditor------%hhu",_isEditor);
    if (_isEditor) {
        
        if (sender.isSelected) {
            
            NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"yes",@"selected",_arr[0],@"shop_id", nil];
            NSNotification *notification =[NSNotification notificationWithName:@"selected_shop" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
        }else{
            
            NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"no",@"selected",_arr[0],@"shop_id", nil];
            NSNotification *notification =[NSNotification notificationWithName:@"selected_shop" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }
    }else{
        
        if (sender.isSelected) {
            
            NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"yes",@"selected",_arr[0],@"shop_id",_arr[1],@"num",_arr[2],@"price", nil];
            NSNotification *notification =[NSNotification notificationWithName:@"pay_selected_shop" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
        }else{
            
            NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"no",@"selected",_arr[0],@"shop_id",_arr[1],@"num",_arr[2],@"price", nil];
            NSNotification *notification =[NSNotification notificationWithName:@"pay_selected_shop" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
